from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://www/baidu.com')

# TODO 定位元素xpath标签和属性
kw = driver.find_element(By.XPATH,'//input[@id="kw"]')
print(kw.get_attribute('outerhtml'))

kw = driver.find_element(By.XPATH,'//input[@class="s_ipt"]')
print(kw.get_attribute('outerHTML'))

kw = driver.find_element(By.XPATH,'//*[@maxlength="255"]')
print(kw.get_attribute('outerHTML'))

input_lists = driver.find_elements(By.XPATH,'//input')
for ipt in input_lists:
    print(ipt.get_attribute('outerHTML'))

# TODO xpath层级定位
ipt = driver.find_element(By.XPATH,'//form/span/input')
print(ipt.get_attribute('outerhtml'))

ipt = driver.find_element(By.XPATH,'//form/span')
print(ipt.get_attribute('outerHTML'))

# TODO xpath标签索引
# //form/input
ipt = driver.find_element(By.XPATH,'//form/input[2]')
print(ipt.get_attribute('outerHTML'))

# TODO xpath逻辑运算使用
print(driver.find_element(By.XPATH,'//input[@id="kw" and @class="s_ipt"]').get_attribute('outerHTML'))

# TODO xpath模糊匹配
hao = driver.find_element(By.XPATH,'//a[contains(text(),"hao")]')
print(hao.get_attribute('outerHTML'))

result = driver.find_element(By.XPATH,'//*[contains(@id,"result")]')
print(result.get_attribute('outerHTML'))

# 退出浏览器
driver.quit()